/*
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.skatiger.jrove.component;

import org.skatiger.jrove.ViewContext;
import org.skatiger.jrove.resource.Resource;
import org.skatiger.jrove.resource.ResourceType;
import org.skatiger.jrove.util.StringUtils;

public class UIResource extends UIComponentBase implements Resource {
    public static final String TARGET_HEAD = "head";
    private String value;
    private ResourceType type;
    private String target;

    public void setTarget(String target) {
        this.target = target;
    }

    public String getTarget() {
        return getPropertyValue("target", target);
    }

    public ResourceType getType() {
        return type;
    }

    public void setType(ResourceType type) {
        this.type = type;
    }

    public String getValue() {
        return getPropertyValue("value", value);
    }

    public void setValue(String value) {
        this.value = value;
    }

    public static String getResourceURL(ViewContext context, String value) {
        String contextPath = (String) context.getExternalContext()
                .getRequestContextPath();
        if (StringUtils.isEmpty(contextPath)) {
            return StringUtils.EMPTY;
        }
        if (value.startsWith("/") && !value.startsWith(contextPath)) {
            return contextPath + value;
        }
        return value;
    }
}
